package com.roncoo.education.course.service.admin.biz;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.ExperimentDao;
import com.roncoo.education.course.dao.ZoneExperimentDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ZoneExperiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ZoneExperimentExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ZoneExperimentExample.Criteria;
import com.roncoo.education.course.service.admin.req.AdminZoneExperimentEditReq;
import com.roncoo.education.course.service.admin.req.AdminZoneExperimentPageReq;
import com.roncoo.education.course.service.admin.req.AdminZoneExperimentSaveReq;
import com.roncoo.education.course.service.admin.resp.AdminExperimentViewResp;
import com.roncoo.education.course.service.admin.resp.AdminZoneCoursePageResp;
import com.roncoo.education.course.service.admin.resp.AdminZoneExperimentPageResp;
import com.roncoo.education.course.service.admin.resp.AdminZoneExperimentViewResp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * ADMIN-专区课程关联表
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AdminZoneExperimentBiz extends BaseBiz {

    @NotNull
    private final ZoneExperimentDao dao;
    @NotNull
    private final ExperimentDao experimentDao;
    /**
     * 专区课程关联表分页
     *
     * @param req 专区课程关联表分页查询参数
     * @return 专区课程关联表分页查询结果
     */
    public Result<Page<AdminZoneExperimentPageResp>> page(AdminZoneExperimentPageReq req) {
    	ZoneExperimentExample example = new ZoneExperimentExample();
        Criteria c = example.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getZoneId())) {
            c.andZoneIdEqualTo(req.getZoneId());
        }
        example.setOrderByClause("sort asc, id desc");
        Page<ZoneExperiment> page = dao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AdminZoneExperimentPageResp> respPage = PageUtil.transform(page, AdminZoneExperimentPageResp.class);
        if (CollUtil.isNotEmpty(respPage.getList())) {
            List<Long> experimentIdList = respPage.getList().stream().map(AdminZoneExperimentPageResp::getExperimentId).collect(Collectors.toList());
            Map<Long, Experiment> experimentMap = experimentDao.listByIds(experimentIdList).stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
            for (AdminZoneExperimentPageResp resp : respPage.getList()) {
                resp.setExperimentViewResp(BeanUtil.copyProperties(experimentMap.get(resp.getExperimentId()), AdminExperimentViewResp.class));
            }
        }
        return Result.success(respPage);
    }
//    public Result<Page<AdminZoneExperimentPageResp>> page(AdminZoneExperimentPageReq req) {
//        ZoneExperimentExample example = new ZoneExperimentExample();
//        Criteria c = example.createCriteria();
//        Page<ZoneExperiment> page = dao.page(req.getPageCurrent(), req.getPageSize(), example);
//        Page<AdminZoneExperimentPageResp> respPage = PageUtil.transform(page, AdminZoneExperimentPageResp.class);
//        return Result.success(respPage);
//    }

    /**
     * 专区课程关联表添加
     *
     * @param req 专区课程关联表
     * @return 添加结果
     */
    public Result<String> save(AdminZoneExperimentSaveReq req) {
        ZoneExperiment record = BeanUtil.copyProperties(req, ZoneExperiment.class);
        if (dao.save(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 专区课程关联表查看
     *
     * @param id 主键ID
     * @return 专区课程关联表
     */
    public Result<AdminZoneExperimentViewResp> view(Long id) {
        return Result.success(BeanUtil.copyProperties(dao.getById(id), AdminZoneExperimentViewResp.class));
    }

    /**
     * 专区课程关联表修改
     *
     * @param req 专区课程关联表修改对象
     * @return 修改结果
     */
    public Result<String> edit(AdminZoneExperimentEditReq req) {
        ZoneExperiment record = BeanUtil.copyProperties(req, ZoneExperiment.class);
        if (dao.updateById(record) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    /**
     * 专区课程关联表删除
     *
     * @param id ID主键
     * @return 删除结果
     */
    public Result<String> delete(Long id) {
        if (dao.deleteById(id) > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }
}
